package com.hbnu.controller;

import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.hbnu.domain.Book;
import com.hbnu.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.Scanner;

/**
 * @version: 1.0
 * @ClassName: index
 * @Description: TODO
 * @Author: 86132
 * @Date: 2025/11/12 8:06
 */
@Component
public class Index {
    private static final int SEARCH_ALL_CHOICE = 1;
    private static final int SEARCH_ONE_CHOICE = 2;
    private static final int BOOK_CHOICE = 3;
    private static final int REJECT_BOOK_CHOICE = 4;
    private static final int LIST_BOOK_CHOICE = 5;
    private static final int EXIT_CHOICE = 6;

    @Autowired
    private Function function;

    public void index(User user) {
        Book book = new Book();
        Scanner sc = new Scanner(System.in);
        int choice;
        int id;
        while (true) {
            functionMenu();
            choice = sc.nextInt();
            switch (choice) {
                case SEARCH_ALL_CHOICE:
                    //查看所有民宿
                    function.searchAll();
                    break;
                case SEARCH_ONE_CHOICE:
                    //查看指定民宿
                    System.out.print("请输入序号：");
                    id = sc.nextInt();
                    function.searchOne(id);
                    break;
                case BOOK_CHOICE:
                    //预约民宿
                    book.setUserId(user.getId());
                    System.out.print("请输入要预约的民宿序号：");
                    book.setAdobeId(sc.nextInt());
                    System.out.print("请输入入住日期(yyyy-MM-dd):");
                    book.setCheckInDate(transform(sc.next()));
                    System.out.print("请输入退房日期(yyyy-MM-dd):");
                    book.setCheckOutDate(transform(sc.next()));
                    function.book(book);
                    System.out.println("预约成功！");
                    break;
                case REJECT_BOOK_CHOICE:
                    //取消预约
                    System.out.print("请输入要取消的订单号:");
                    id = sc.nextInt();
                    function.deleteBook(id);
                    break;
                case LIST_BOOK_CHOICE:
                    //查看用户预约
                    function.listByUserId(user.getId());
                    break;
                case EXIT_CHOICE:
                    System.out.println("谢谢惠顾");
                    System.exit(0);
                    break;
                default:
                    System.out.println("没有此功能，请重新输入！");
                    break;
            }
        }

    }

    //日期转换
    public Date transform(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            //？？？？？？？？？？？？？？？？？？？？？？？？？？？？异常处理
            System.out.println("日期格式有误！");
            return null;
        }
    }

    //显示功能菜单
    public void functionMenu() {
        System.out.println("=======欢迎光临民宿预约系统==========");
        System.out.println("1.查看所有民宿");
        System.out.println("2.查看指定民宿");
        System.out.println("3.预约民宿");
        System.out.println("4.取消预约");
        System.out.println("5.查看预约信息");
        System.out.println("6.退出");
        System.out.println("================================");
        System.out.print("请选择功能：");
    }
}
